import json
import re
import plotly.graph_objects as go

# 解析字符串函数
def parse_f64point_string(point_string):
    # 使用正则表达式匹配 F64Point 中的坐标
    pattern = r"F64Point\s*{\s*x:\s*([0-9.]+),\s*y:\s*([0-9.]+)\s*}"
    
    # 查找所有匹配的坐标
    matches = re.findall(pattern, point_string)
    
    # 将字符串中的坐标转换为浮点数，并构造包含字典的列表
    points = [{"x": float(x), "y": float(y)} for x, y in matches]
    
    return points

# 示例字符串
line_segment_string = "Some([F64Point { x: 28193.0, y: 8100.0 }, F64Point { x: 28051.515625, y: 9045.453125 }, F64Point { x: 28127.26953125, y: 9475.755859375 }, F64Point { x: 28520.349609375, y: 10751.0849609375 }, F64Point { x: 28595.736328125, y: 11338.8447265625 }, F64Point { x: 28609.681640625, y: 11679.8095703125 }, F64Point { x: 27357.80078125, y: 12906.599609375 }, F64Point { x: 27209.80078125, y: 13106.599609375 }, F64Point { x: 26936.46875, y: 13499.93359375 }, F64Point { x: 26905.80078125, y: 13717.267578125 }, F64Point { x: 26886.099609375, y: 13942.5498046875 }, F64Point { x: 27018.6484375, y: 14168.78515625 }, F64Point { x: 29592.099609375, y: 16200.2998046875 }, F64Point { x: 29811.4921875, y: 16364.29296875 }, F64Point { x: 30208.46875, y: 16369.265625 }, F64Point { x: 30348.46875, y: 16305.265625 }, F64Point { x: 31597.80078125, y: 15666.599609375 }, F64Point { x: 32299.1328125, y: 15231.93359375 }, F64Point { x: 33064.4609375, y: 14402.5986328125 }, F64Point { x: 33321.80078125, y: 14354.599609375 }, F64Point { x: 33600.7109375, y: 14417.0654296875 }, F64Point { x: 33771.1328125, y: 14545.2646484375 }, F64Point { x: 34496.46875, y: 15279.9345703125 }, F64Point { x: 38767.02734375, y: 19044.685546875 }, F64Point { x: 39049.859375, y: 19186.76953125 }, F64Point { x: 40289.44140625, y: 19589.978515625 }, F64Point { x: 40472.5, y: 19667.94921875 }, F64Point { x: 40497.80078125, y: 19822.599609375 }, F64Point { x: 40492.46875, y: 20189.265625 }, F64Point { x: 40411.1015625, y: 20353.25 }, F64Point { x: 40236.5, y: 20525.94921875 }, F64Point { x: 39176.80078125, y: 20774.599609375 }, F64Point { x: 38364.0, y: 21032.0 }, F64Point { x: 36464.78125, y: 22232.91015625 }, F64Point { x: 36356.43359375, y: 22370.583984375 }, F64Point { x: 36439.1015625, y: 22601.25 }, F64Point { x: 37075.58984375, y: 23208.404296875 }, F64Point { x: 37385.953125, y: 23394.625 }, F64Point { x: 37857.0, y: 23600.0 }])"

# 调用解析函数
line_segment = parse_f64point_string(line_segment_string)

# 提取线段的 x, y 坐标
line_x = [point["x"] for point in line_segment]
line_y = [point["y"] for point in line_segment]

# 读取 JSON 文件
with open('map0101_data.json') as f:  # 将 'test_walkable.json' 替换为你的实际文件名
    data = json.load(f)
data = data["navmesh"]
# 提取顶点坐标
points = data["points"]

# 提取 points 列表中的所有点的 x, y 坐标
x_coords = [point["x"] for point in points]
y_coords = [point["y"] for point in points]

# 创建图形
fig = go.Figure()

# 提取多边形并绘制
for polygon in data["polygons"]:
    point_indices = polygon["point_indices"]
    
    # 使用索引提取多边形的顶点坐标
    polygon_x = [x_coords[i] for i in point_indices]
    polygon_y = [y_coords[i] for i in point_indices]

    # 为了闭合多边形，将第一个点添加到最后
    polygon_x.append(polygon_x[0])
    polygon_y.append(polygon_y[0])

    # 绘制多边形
    fig.add_trace(go.Scatter(
        x=polygon_x, 
        y=polygon_y, 
        fill='toself',  # 填充多边形
        mode='lines+markers',  # 显示线条和标记点
        name="Polygon",
        hovertemplate='X: %{x:.6f}<br>Y: %{y:.6f}<extra></extra>',  # 自定义显示完整的坐标，显示6位小数
    ))

# 绘制解析后的线段
fig.add_trace(go.Scatter(
    x=line_x,
    y=line_y,
    mode='lines+markers',
    name='Line Segment',
    line=dict(color='red', width=2),  # 自定义线条颜色和宽度
    marker=dict(size=8, color='blue'),  # 自定义标记点大小和颜色
    hovertemplate='X: %{x:.6f}<br>Y: %{y:.6f}<extra></extra>',  # 自定义显示完整的坐标，显示6位小数
))

test_points = [
    {"x": 29036.242, "y": 16360.97},
    {"x": 29208.969, "y": 16360.97},
    {"x": 29208.969, "y": 16270.061},
    {"x": 29036.242, "y": 16270.061},
]

test_x = [point["x"] for point in test_points]
test_y = [point["y"] for point in test_points]

test_x.append(test_x[0])
test_y.append(test_y[0])

fig.add_trace(go.Scatter(
    x=test_x,
    y=test_y,
    fill='toself',  # Fill the polygon
    mode='lines+markers',  # Display lines and points
    name="Test Points Polygon",
    line=dict(color='green', width=2),  # Customize line color and width
    marker=dict(size=8, color='orange'),  # Customize point size and color
    hovertemplate='X: %{x:.6f}<br>Y: %{y:.6f}<extra></extra>',  # Display detailed coordinates
))

# 设置图形的标题和坐标轴标签
fig.update_layout(
    title="Polygons and Line Segment from JSON Data",
    xaxis_title="X Axis",
    yaxis_title="Y Axis",
    showlegend=True,
    xaxis=dict(tickformat=".6f"),  # 强制显示完整的X坐标
    yaxis=dict(tickformat=".6f")   # 强制显示完整的Y坐标
)

# 显示图表
fig.show()
